﻿using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using static PengSW.WebHelper.Downloader;

namespace AutoScript
{
    class Program
    {
        static void Main(string[] _)
        {
            try
            {
                string aUrl = "https://www.miaojiang8.com/0_289/";
                string aHomePage = DownloadString(aUrl, Encoding.Default);
                MatchCollection aMatchs = Regex.Matches(aHomePage, @"<a href=""(/20_20864/\d+.html)"">");
                var aLinks = (from Match r in aMatchs select r.Groups[1].Value).ToArray();
                aLinks = (from r in aLinks orderby r select r).Distinct().ToArray();

                foreach (var aLink in aLinks)
                {
                    Console.WriteLine(aLink);
                    string aPage = DownloadString($"http://www.ttkxs.org{aLink}", Encoding.Default);
                    Match aMatch = Regex.Match(aPage, @"<div id=""content"">(.*?)</div>", RegexOptions.Singleline);
                    string aCapter = aMatch.Groups[1].Value;
                    aCapter = aCapter.Replace("&nbsp;", " ");
                    aCapter = aCapter.Replace("<br />", "");
                    aCapter = aCapter.Replace("<br>", "");
                    File.AppendAllText("yi.txt", aCapter, Encoding.Default);
                    Thread.Sleep(2000);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.WriteLine("Press RETURN to exit...");
            Console.ReadLine();
        }
    }
}
